package org.me.marble;

import java.nio.*;

import javax.microedition.khronos.opengles.GL10;

/**
 *
 * @author tungi
 */

public class LifeObject extends AvailableObject{
    private FloatBuffer frontBuffer,backBuffer,blanketBuffer;
    private FloatBuffer frontTextureBuffer,backTextureBuffer;

    public LifeObject(int id){
        super(id);
    }

    @Override
    public void effect(GameRenderer _renderer,MoveMarble _mm){
        OptionsLibrary.lifes++;
    }

    @Override
    public void init(){
        float[] front_side={-0.25f,0.3f,-0.02f,
                            0.25f,0.3f,-0.02f,
                            -0.25f,-0.3f,-0.02f,
                            0.25f,-0.3f,-0.02f
        };
        float[] back_side={-0.25f,0.3f,0.02f,
                           -0.25f,-0.3f,0.02f,
                           0.25f,0.3f,0.02f,
                           0.25f,-0.3f,0.02f
        };
        float[] blanket={-0.25f,0.3f,-0.02f,
                         -0.25f,0.3f,0.02f,
                         0.25f,0.3f,-0.02f,
                         0.25f,0.3f,0.02f,
                         0.25f,-0.3f,-0.02f,
                         0.25f,-0.3f,0.02f,
                         -0.25f,-0.3f,-0.02f,
                         -0.25f,-0.3f,0.02f,
                         -0.25f,0.3f,-0.02f,
                         -0.25f,0.3f,0.02f
        };
        float[] fronttextures={0.0f,1.0f, 1.0f,1.0f, 0.0f,0.0f, 1.0f,0.0f};
        float[] backtextures={0.0f,1.0f, 0.0f,0.0f, 1.0f,1.0f, 1.0f,0.0f};

        frontBuffer=BufferFactory.MakeFloatBuffer(front_side);
        backBuffer=BufferFactory.MakeFloatBuffer(back_side);
        blanketBuffer=BufferFactory.MakeFloatBuffer(blanket);
        frontTextureBuffer=BufferFactory.MakeFloatBuffer(fronttextures);
        backTextureBuffer=BufferFactory.MakeFloatBuffer(backtextures);

        color=new MyColor();
        color.setAmbient(new float[]{(float)227/255,(float)197/255,(float)77/255,0.8f});
        color.setDiffuse(new float[]{(float)227/255,(float)197/255,(float)77/255,0.8f});
        color.setSpecular(new float[]{0.6f,0.6f,0.6f,0.7f});
        color.setEmission(new float[]{0.1f,0.1f,0.1f,0.6f});
        color.setShininess(1.0f);
    }
    
    @Override
    public void draw(GL10 gl){
        transform(gl);
        gl.glRotatef(30,1,0,0);

        OpenGLSystem.enable(gl,OpenGLSystem.CULL_FACE | OpenGLSystem.TEXTURE);
        OpenGLSystem.enableClientStates(gl,OpenGLSystem.NORMAL_ARRAY |
                OpenGLSystem.VERTEX_ARRAY | OpenGLSystem.TEXTURE_ARRAY);

        OpenGLSystem.setMaterial(gl,color);

        OpenGLSystem.drawTriangles(gl,texture,frontBuffer,frontBuffer,
                frontTextureBuffer,4,GL10.GL_TRIANGLE_STRIP);
        OpenGLSystem.drawTriangles(gl,texture,backBuffer,backBuffer,
                backTextureBuffer,4,GL10.GL_TRIANGLE_STRIP);
        
        OpenGLSystem.disableClientStates(gl,OpenGLSystem.TEXTURE_ARRAY);
        OpenGLSystem.disable(gl,OpenGLSystem.TEXTURE);

        OpenGLSystem.drawTriangles(gl,null,blanketBuffer,blanketBuffer,
                null,10,GL10.GL_TRIANGLE_STRIP);

        OpenGLSystem.disableClientStates(gl,OpenGLSystem.NORMAL_ARRAY | 
                OpenGLSystem.VERTEX_ARRAY);
        OpenGLSystem.disable(gl,OpenGLSystem.CULL_FACE);
    }
}
